/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import dao.ClasseDAO;
import dao.ClienteDAO;
import dao.LocalizaCidadeDAO;
import java.awt.event.KeyEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;
import model.*;

/**
 *
 * @author Loan
 */
public class ClienteFrame extends javax.swing.JFrame {

    /**
     * Creates new form ClienteFrame
     */
    public ClienteFrame() {
        initComponents();
        buscarTodos();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel14 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        btnSalvar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cmbTipoPessoa = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        tbNome = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        tbLogradouro = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        tbComplemento = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        tbBairro = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        tbCidade = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        cmbStatus = new javax.swing.JComboBox();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        tbObs = new javax.swing.JTextField();
        tbDataNascimento = new javax.swing.JFormattedTextField();
        tbCEP = new javax.swing.JFormattedTextField();
        tbTelefone = new javax.swing.JFormattedTextField();
        tbClienteDesde = new javax.swing.JFormattedTextField();
        tbCPF = new javax.swing.JFormattedTextField();
        jLabel16 = new javax.swing.JLabel();
        tbEstado = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        tbPais = new javax.swing.JTextField();
        tbNumero = new javax.swing.JFormattedTextField();
        jLabel18 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        tbDataDesativa = new javax.swing.JFormattedTextField();
        tbCNPJ = new javax.swing.JFormattedTextField();
        tbIdEstrangeiro = new javax.swing.JTextField();
        tbCodigoCidade = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem12 = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JPopupMenu.Separator();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem8 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jMenuItem9 = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuItem10 = new javax.swing.JMenuItem();
        Relatório = new javax.swing.JMenu();
        jMenuItem13 = new javax.swing.JMenuItem();
        jMenuItem14 = new javax.swing.JMenuItem();
        jMenuItem15 = new javax.swing.JMenuItem();
        jMenuItem16 = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem7 = new javax.swing.JMenuItem();

        jLabel14.setText("jLabel14");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastro de Clientes");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });
        addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                formFocusGained(evt);
            }
        });

        jTable1.setAutoCreateRowSorter(true);
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Tipo Pessoa", "CPF/CNPJ/Estr", "Nome Completo", "Data Nascimento", "Endereço", "Cidade", "Projetos", "Status"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Integer.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setRowSelectionAllowed(false);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setResizable(false);
        jTable1.getColumnModel().getColumn(6).setResizable(false);
        jTable1.getColumnModel().getColumn(7).setResizable(false);

        btnSalvar.setText("Salvar");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Tipo Pessoa:");

        cmbTipoPessoa.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Física", "Jurídica", "Estrangeiro" }));
        cmbTipoPessoa.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbTipoPessoaItemStateChanged(evt);
            }
        });
        cmbTipoPessoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbTipoPessoaActionPerformed(evt);
            }
        });
        cmbTipoPessoa.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                cmbTipoPessoaFocusLost(evt);
            }
        });

        jLabel2.setText("CPF/CNPJ/ID Estrangeiro:");

        jLabel3.setText("Nome Completo:");

        jLabel4.setText("Data Nascimento:");

        jLabel5.setText("Logradouro:");

        jLabel6.setText("Complemento:");

        jLabel7.setText("Bairro:");

        jLabel8.setText("CEP:");

        jLabel9.setText("Cidade:");

        tbCidade.setEditable(false);

        jLabel10.setText("Status:");

        cmbStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Inativo", "Ativo" }));
        cmbStatus.setSelectedIndex(1);
        cmbStatus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbStatusActionPerformed(evt);
            }
        });

        jLabel11.setText("Telefone:");

        jLabel12.setText("Cliente desde:");

        jLabel13.setText("Outras Informações:");

        try {
            tbDataNascimento.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        try {
            tbCEP.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##.###-###")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        try {
            tbTelefone.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##) ####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        try {
            tbClienteDesde.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        try {
            tbCPF.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jLabel16.setText("Estado:");

        tbEstado.setEnabled(false);

        jLabel17.setText("País:");

        tbPais.setEnabled(false);

        tbNumero.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(java.text.NumberFormat.getIntegerInstance())));

        jLabel18.setText("Número:");

        jLabel19.setText("Data desativação:");

        try {
            tbDataDesativa.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        try {
            tbCNPJ.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##.###.###/####-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        tbCodigoCidade.setNextFocusableComponent(tbTelefone);
        tbCodigoCidade.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tbCodigoCidadeFocusLost(evt);
            }
        });
        tbCodigoCidade.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tbCodigoCidadeKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(tbBairro))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(tbCEP, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel18)
                            .addComponent(tbNumero, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(tbDataNascimento)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(cmbTipoPessoa, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel5)
                                    .addComponent(tbCPF))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(tbNome)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addGap(0, 0, Short.MAX_VALUE))))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(tbLogradouro)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(tbComplemento)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel6)
                                        .addGap(0, 0, Short.MAX_VALUE))))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(cmbStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.LEADING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel19, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(tbDataDesativa, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 63, Short.MAX_VALUE)
                        .addComponent(tbIdEstrangeiro, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tbCNPJ, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11)
                            .addComponent(tbTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(tbClienteDesde, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(tbObs)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel13)
                                        .addGap(0, 0, Short.MAX_VALUE))))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(119, 119, 119)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(tbEstado)
                                    .addComponent(jLabel16))
                                .addGap(157, 157, 157))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(tbCodigoCidade, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tbCidade, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel17)
                                .addGap(118, 118, 118))
                            .addComponent(tbPais, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbTipoPessoa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tbNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tbCPF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tbLogradouro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tbComplemento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tbDataNascimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(jLabel18))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tbBairro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tbCEP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tbNumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel16)
                            .addComponent(jLabel9))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tbCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(tbCodigoCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel17)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tbPais, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(jLabel12)
                    .addComponent(jLabel13))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tbObs, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tbTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tbClienteDesde, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel19)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tbDataDesativa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tbCNPJ, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tbIdEstrangeiro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel15.setText("Clientes cadastrados:");

        jMenu1.setText("Cadastros");

        jMenuItem1.setText("Clientes");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem12.setText("Usuários");
        jMenuItem12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem12ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem12);
        jMenu1.add(jSeparator3);

        jMenuItem2.setText("Casses");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuItem3.setText("Famílias");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuItem8.setText("Produtos");
        jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem8ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem8);
        jMenu1.add(jSeparator1);

        jMenuItem9.setText("Localização");
        jMenuItem9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem9ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem9);
        jMenu1.add(jSeparator2);

        jMenuItem4.setText("Sair");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem4);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Projetos");

        jMenuItem5.setText("Gerenciar Projetos");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem5);

        jMenuItem6.setText("Consultar Projetos");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem6);

        jMenuItem10.setText("Imprimir Documento");
        jMenu2.add(jMenuItem10);

        jMenuBar1.add(jMenu2);

        Relatório.setText("Relatórios");

        jMenuItem13.setText("Relatório de Classes/Famílias");
        Relatório.add(jMenuItem13);

        jMenuItem14.setText("Relatório de Materiais");
        Relatório.add(jMenuItem14);

        jMenuItem15.setText("Relatório de Projetos");
        Relatório.add(jMenuItem15);

        jMenuItem16.setText("Relatório de Clientes");
        Relatório.add(jMenuItem16);

        jMenuBar1.add(Relatório);

        jMenu4.setText("Sobre");
        jMenuBar1.add(jMenu4);

        jMenu3.setText("Sair");

        jMenuItem7.setText("Sair");
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem7);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnSalvar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelar))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel15)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel15)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btnSalvar))
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-691)/2, (screenSize.height-719)/2, 691, 719);
    }// </editor-fold>//GEN-END:initComponents

    private void setStatusInicial() {

        status = 'I';

        tbDataDesativa.setEnabled(false);
        
        tbCNPJ.setEnabled(true);
        tbCPF.setEnabled(true);
        tbIdEstrangeiro.setEnabled(true);
               
        tbDataDesativa.setEnabled(false);        
        
        cmbTipoPessoa.setEnabled(true);
        cmbTipoPessoa.requestFocus();
        
    }

    private void setStatusAlterar() {

        status = 'A';
        tbCNPJ.setEnabled(false);
        tbCPF.setEnabled(false);
        tbIdEstrangeiro.setEnabled(false);        
        cmbTipoPessoa.setEnabled(false);
    }

    private void limparCampos() {
        tbBairro.setText("");
        tbCEP.setText("");

        tbCPF.setText("");
        tbIdEstrangeiro.setText("");
        tbCNPJ.setText("");

        tbDataDesativa.setText("");        
        tbCidade.setText("");
        tbCodigoCidade.setText("");
        tbClienteDesde.setText("");
        tbComplemento.setText("");
        tbDataNascimento.setText("");
        tbEstado.setText("");
        tbLogradouro.setText("");
        tbNome.setText("");
        tbNumero.setText("");
        tbObs.setText("");
        tbPais.setText("");
        tbTelefone.setText("");
    }

    private void buscarTodos() {
        // Carrega todos os dados da tabela classe                 
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        List<Cliente> lista = new ClienteDAO().findAll();
        DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
        modelo.setNumRows(0);
        int i = 0;

        for (Cliente m : lista) {
            System.out.println("FOR");
            try {
                System.out.println("Tipo pes number-> "+m.getTipoPessoa());
                System.out.println("Tipo pes descrit-> "+m.getTipoPessoaDescricao());
                modelo.addRow(new String[]{"" + m.getTipoPessoaDescricao(), m.getCpf_cnpj(), m.getNomeCompleto(), sdf.format(m.getDataNascimento()) + "", m.getLogradouro() + ", " + m.getBairro() + ", " + m.getComplemento(), m.getCidade().getCidade_descricao(), cdao.contarProjetosDoCliente(lista.get(i)) + "", m.getStatusDescritivo() + ""});
            } catch (java.lang.NullPointerException ex) {
                System.out.println("CATCH!!!!!!!!!");
            }
            i++;
            System.out.println("i -> " + i);
        }        
    }

    private boolean verificaCamposNulos() {
        if (tbCPF.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Campo CPF/CNPJ/ID em branco!");
            tbCPF.requestFocus();
            return false;
        }
        if (tbNome.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Campo nome em branco!");
            tbNome.requestFocus();
            return false;
        }
        return true;

    }

    private String formatarData(Date data) {
        String data_formatado = null;
        if (data != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            data_formatado = sdf.format(data);
            System.out.println("data intput- >" + data.toString());
            System.out.println("data output- >" + data_formatado);
        }
        return data_formatado;
    }

    private void selecionar() {

        // obtem o valor da linha selecionada

        limparCampos();
        int selecionada = jTable1.getSelectedRow();

        ClientePK cpk = new ClientePK();

        // obtem o valor da coluna        
        cpk.setTipoPessoaByDescricao((String) jTable1.getValueAt(selecionada, 0));
        System.out.println("Tipo pessoa -> "+cpk.getTipoPessoa());
        cpk.setCpf_cnpj((String) jTable1.getValueAt(selecionada, 1));

        System.out.println("Selecionou cpf -> " + cpk.getCpf_cnpj());

        cli = cdao.findById(cpk);
        tbBairro.setText("" + cli.getBairro());
        tbCEP.setText("" + cli.getCep());

        tbCodigoCidade.setText(""+cli.getCidade().getCidade_id());
        tbCidade.setText("" + cli.getCidade().getCidade_descricao());
        tbEstado.setText(""+cli.getCidade().getEstado_cidade().getEstado_descricao());
        tbPais.setText(""+cli.getCidade().getEstado_cidade().getPais_estado().getPais_descricao());

        tbComplemento.setText("" + cli.getComplemento());

        
        tbLogradouro.setText("" + cli.getLogradouro());
        tbNome.setText("" + cli.getNomeCompleto());
        tbNumero.setText("" + cli.getNumero());
        tbObs.setText("" + cli.getOutrasInformacoes());
        
        tbTelefone.setText("" + cli.getTelefoneContato());

        if (cli.getDataNascimento() != null) {
            tbDataNascimento.setText(formatarData(cli.getDataNascimento()));
        }
        if (cli.getDataCadastro() != null) {
            tbClienteDesde.setText(formatarData(cli.getDataCadastro()));
        }
        if (cli.getDataDesativa() != null) {
            tbDataDesativa.setText(formatarData(cli.getDataDesativa()));
        }
        setarMascaraCPFCNPJ(cli.getTipoPessoa());
        
        switch (cli.getTipoPessoa()) {
            case 0:
                tbCPF.setText("" + cli.getCpf_cnpj().trim());
                break;
            case 1:
                tbCNPJ.setText("" + cli.getCpf_cnpj().trim());
                break;
            default:
                tbIdEstrangeiro.setText("" + cli.getCpf_cnpj().trim());
                break;
        }
        
        cmbTipoPessoa.setSelectedIndex(cli.getTipoPessoa());
        cmbStatus.setSelectedIndex(cli.getStatus());
        switch (cli.getStatus()) {
            case 0:
                tbDataDesativa.setEnabled(true);
                break;
            case 1:
                tbDataDesativa.setEnabled(false);
        }

        setStatusAlterar();
    }

    private void setarDadosDoCliente() {

        
        
        cli.setNomeCompleto(tbNome.getText());
        cli.setBairro(tbBairro.getText());
        cli.setCep(Integer.parseInt(tbCEP.getText().substring(0, 2) + tbCEP.getText().substring(3, 6) + tbCEP.getText().substring(7, 10)));
        cidade.setCidade_id(tbCodigoCidade.getText());
        cli.setCidade(cidade);
        cli.setComplemento(tbComplemento.getText());
        try {
            cli.setDataCadastro(
                    new SimpleDateFormat("dd/MM/yyyy").parse(
                    tbClienteDesde.getText()));
        } catch (ParseException f) {
            System.out.println("" + f.getMessage());
        }
        try {
            cli.setDataNascimento(
                    new SimpleDateFormat("dd/MM/yyyy").parse(
                    tbDataNascimento.getText()));
        } catch (ParseException f) {
            System.out.println("" + f.getMessage());
        }
        if (!tbDataDesativa.getText().isEmpty()) {
            try {
                cli.setDataDesativa(
                        new SimpleDateFormat("dd/MM/yyyy").parse(
                        tbDataDesativa.getText()));
            } catch (ParseException f) {
                System.out.println("" + f.getMessage());
            }
        }
        else{
            cli.setDataDesativa(null);
        }
        cli.setLogradouro(tbLogradouro.getText());
        cli.setOutrasInformacoes(tbObs.getText());
        cli.setStatus(cmbStatus.getSelectedIndex());
        cli.setTelefoneContato(tbTelefone.getText());
        cli.setTipoPessoa(cmbTipoPessoa.getSelectedIndex());
        cli.setNumero(Integer.parseInt(tbNumero.getText()));
    }

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed

        switch (status) {
            case 'I': // Status de  Inserir
                cli = new Cliente();
                setarDadosDoCliente();
                switch(cmbTipoPessoa.getSelectedIndex()){
                    case 0: 
                        cli.setCpf_cnpj(getNumerosCPF(tbCPF.getText()));
                        break;
                    case 1:
                        cli.setCpf_cnpj(getNumerosCNPJ(tbCNPJ.getText()));
                        break;
                    case 2:    
                        cli.setCpf_cnpj(tbIdEstrangeiro.getText());
                        break;
                }                  

                if (verificaCamposNulos()) {
                    cdao.insert(cli);
                    buscarTodos();
                    limparCampos();
                    setStatusInicial();
                }
                break;

            case 'A': // Status de Alterar
                setarDadosDoCliente();
                if (verificaCamposNulos()) {
                    cdao.update(cli);
                    buscarTodos();
                    limparCampos();
                    setStatusInicial();
                }
                break;
        }

    }//GEN-LAST:event_btnSalvarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        limparCampos();
        buscarTodos();
        setStatusInicial();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        ClienteFrame cf = new ClienteFrame();
        cf.setVisible(true);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem12ActionPerformed
        UsuarioFrame cf = new UsuarioFrame();
        cf.setVisible(true);
    }//GEN-LAST:event_jMenuItem12ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        ClasseFrame cf = new ClasseFrame();
        cf.setVisible(true);
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        FamiliaFrame cf = new FamiliaFrame();
        cf.setVisible(true);
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem8ActionPerformed
        MaterialFrameTab1 cf = new MaterialFrameTab1();
        cf.setVisible(true);
    }//GEN-LAST:event_jMenuItem8ActionPerformed

    private void jMenuItem9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem9ActionPerformed
        LocalizaFrame cf = new LocalizaFrame();
        cf.setVisible(true);
    }//GEN-LAST:event_jMenuItem9ActionPerformed

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        System.exit(1);
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
        ProjetoFrame cf = new ProjetoFrame();
        cf.setVisible(true);
    }//GEN-LAST:event_jMenuItem5ActionPerformed

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
   }//GEN-LAST:event_jMenuItem6ActionPerformed

    private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem7ActionPerformed
        System.exit(1);
    }//GEN-LAST:event_jMenuItem7ActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        selecionar();
    }//GEN-LAST:event_jTable1MouseClicked

    private void cmbTipoPessoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbTipoPessoaActionPerformed
        //setarMascaraCPFCNPJ(cmbTipoPessoa.getSelectedIndex());
    }//GEN-LAST:event_cmbTipoPessoaActionPerformed

    private void cmbTipoPessoaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbTipoPessoaItemStateChanged
    }//GEN-LAST:event_cmbTipoPessoaItemStateChanged

    private void cmbTipoPessoaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cmbTipoPessoaFocusLost
        setarMascaraCPFCNPJ(cmbTipoPessoa.getSelectedIndex());
    }//GEN-LAST:event_cmbTipoPessoaFocusLost

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        tbCNPJ.setVisible(false);
        tbIdEstrangeiro.setVisible(false);
        tbDataDesativa.setEnabled(false);
    }//GEN-LAST:event_formWindowOpened

    private void cmbStatusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbStatusActionPerformed
        switch (cmbStatus.getSelectedIndex()) {
            case 0:
                tbDataDesativa.setEnabled(true);
                break;
            case 1:
                tbDataDesativa.setText("");
                tbDataDesativa.setEnabled(false);
                break;
        }
    }//GEN-LAST:event_cmbStatusActionPerformed

    private void formFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_formFocusGained
        
    }//GEN-LAST:event_formFocusGained

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
      buscarTodos();
    }//GEN-LAST:event_formWindowActivated

    private void tbCodigoCidadeFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tbCodigoCidadeFocusLost
        if(!tbCodigoCidade.getText().isEmpty()){
        cidadedao = new  LocalizaCidadeDAO();
        cidade = cidadedao.findById(tbCodigoCidade.getText());        
        tbCidade.setText(cidade.getCidade_descricao());
        tbEstado.setText(cidade.getEstado_cidade().getEstado_descricao());
        tbPais.setText(cidade.getEstado_cidade().getPais_estado().getPais_descricao());
        }
        
    }//GEN-LAST:event_tbCodigoCidadeFocusLost

    private void tbCodigoCidadeKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbCodigoCidadeKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_F2) {
            CidadeDialog popup = new CidadeDialog(null, true);

            popup.setVisible(true);

            LocalizaCidade m = popup.getCidade();
            System.out.println(" ID cidade -> " + m.getCidade_id());
            if (!m.getCidade_id().trim().isEmpty()) {
                tbCodigoCidade.setText(""+m.getCidade_id());
                tbCidade.setText(""+m.getCidade_descricao());
                tbEstado.setText(""+m.getEstado_cidade().getEstado_descricao());
                tbPais.setText(m.getEstado_cidade().getPais_estado().getPais_descricao());
            } else {
                setStatusInicial();
                tbCodigoCidade.setText("");
                tbCidade.setText("");
                tbEstado.setText("");
                tbPais.setText("");                
            }
            popup.dispose();
        }
        
        
        
    }//GEN-LAST:event_tbCodigoCidadeKeyPressed

    private String getNumerosCPF(String cpfComMascara) {
        System.out.println("CPF com máscara INPUT -> " + cpfComMascara);
        System.out.println("CPF sem máscara OUTPUT -> " + cpfComMascara.substring(0, 3) + cpfComMascara.substring(4, 7) + cpfComMascara.substring(8, 11) + cpfComMascara.substring(12, 14));
        return (cpfComMascara.substring(0, 3) + cpfComMascara.substring(4, 7) + cpfComMascara.substring(8, 11) + cpfComMascara.substring(12, 14));
    }

    private String getNumerosCNPJ(String cnpjComMascara) {
        System.out.println("CNPJ com máscara INPUT -> " + cnpjComMascara);
        System.out.println("CNPJ sem máscara OUTPUT -> " + cnpjComMascara.substring(0, 2) + cnpjComMascara.substring(3, 6) + cnpjComMascara.substring(7, 10) + cnpjComMascara.substring(11, 15) + cnpjComMascara.substring(16, 18));
        return (cnpjComMascara.substring(0, 2) + cnpjComMascara.substring(3, 6) + cnpjComMascara.substring(7, 10) + cnpjComMascara.substring(11, 15) + cnpjComMascara.substring(16, 18));
    }

    private String cpfToCnpj(String formatedCPF) {
        System.out.println("CPF com máscara INPUT -> " + formatedCPF);
        String cpf = getNumerosCPF(formatedCPF) + "000";
        // 00012345678900
        System.out.println("CPF com 000 -> " + cpf);
        String cnpj = cpf.substring(0, 2) + "." + cpf.substring(2, 5) + "." + cpf.substring(5, 8) + "/" + cpf.substring(8, 12) + "-" + cpf.substring(12, 14);
        System.out.println("CNPJ com máscara (return)-> " + cnpj);
        System.out.println("CNPJ sem máscara -> " + getNumerosCNPJ(cnpj));
        return cnpj;

    }

    private void setarMascaraCPFCNPJ(int index) {
        switch (index) {
            case 0:
                tbCNPJ.setVisible(false);
                tbIdEstrangeiro.setVisible(false);
                tbCPF.setVisible(true);
                break;
            case 1:
                tbCPF.setVisible(false);
                tbIdEstrangeiro.setVisible(false);
                tbCNPJ.setVisible(true);
                tbCNPJ.setLocation(tbCPF.getLocation());
                break;
            default:
                tbCPF.setVisible(false);
                tbCNPJ.setVisible(false);
                tbIdEstrangeiro.setVisible(true);
                tbIdEstrangeiro.setLocation(tbCPF.getLocation());
                break;
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;


                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ClienteFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ClienteFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ClienteFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ClienteFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ClienteFrame().setVisible(true);

            }
        });
    }
    
    LocalizaCidade cidade = new LocalizaCidade();
    LocalizaCidadeDAO cidadedao = new LocalizaCidadeDAO();
    Cliente cli = new Cliente();
    ClienteDAO cdao = new ClienteDAO();
    private char status = 'I';
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu Relatório;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JComboBox cmbStatus;
    private javax.swing.JComboBox cmbTipoPessoa;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem10;
    private javax.swing.JMenuItem jMenuItem12;
    private javax.swing.JMenuItem jMenuItem13;
    private javax.swing.JMenuItem jMenuItem14;
    private javax.swing.JMenuItem jMenuItem15;
    private javax.swing.JMenuItem jMenuItem16;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JMenuItem jMenuItem9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JPopupMenu.Separator jSeparator3;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField tbBairro;
    private javax.swing.JFormattedTextField tbCEP;
    private javax.swing.JFormattedTextField tbCNPJ;
    private javax.swing.JFormattedTextField tbCPF;
    private javax.swing.JTextField tbCidade;
    private javax.swing.JFormattedTextField tbClienteDesde;
    private javax.swing.JTextField tbCodigoCidade;
    private javax.swing.JTextField tbComplemento;
    private javax.swing.JFormattedTextField tbDataDesativa;
    private javax.swing.JFormattedTextField tbDataNascimento;
    private javax.swing.JTextField tbEstado;
    private javax.swing.JTextField tbIdEstrangeiro;
    private javax.swing.JTextField tbLogradouro;
    private javax.swing.JTextField tbNome;
    private javax.swing.JFormattedTextField tbNumero;
    private javax.swing.JTextField tbObs;
    private javax.swing.JTextField tbPais;
    private javax.swing.JFormattedTextField tbTelefone;
    // End of variables declaration//GEN-END:variables
}
